home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_c / api_shar / sharea.c < prev    next >
Text File  |  1989-01-25  |  5KB  |  187 lines

  1. /****************************************************************
  2. *
  3. *  Name:          SHAREA
  4. *
  5. *  Function:      share memory/data among multiple processes
  6. *
  7. *  Shows how to:  1. allocate and deallocate shared memory.
  8. *                 2. read from and write to shared memory.
  9. *                 3. mail to another process the address of shared data.
  10. *                 4. control access to shared data via mailbox semaphore.
  11. *
  12. *  Written by:    Larry Rush, Quarterdeck Office Systems
  13. *
  14. *  Contact:       Voice:  (213) 392-9851, (213) 392-9701
  15. *                 BBS:    (213) 396-3904, (213) 392-2278
  16. *                 Fax:    (213) 399-3802
  17. *
  18. ****************************************************************/
  19.  
  20. #include <stdio.h>
  21. #include "dvapi.h"
  22.  
  23. /* minimum API version required */
  24. #define required 0x201
  25.  
  26. /* PIF-related constants */
  27. #define PIFNAME "SHAREB.DVP"
  28. #define PIFLEN  416
  29.  
  30. /* arbitrary # times to read/write shared memory */
  31. #define REPS 4
  32.  
  33. /* API version number */
  34. int version;
  35.  
  36. /* default object handle */
  37. ulong win;
  38.  
  39. /* PIF-related variables */
  40. FILE *fp,*fopen();
  41. char dvpbuf[PIFLEN];
  42. char pathbuf[64];
  43.  
  44. /* application handle of other process */
  45. ulong apphanb;
  46.  
  47. /* read/write loop control variable */
  48. int i;
  49.  
  50. /* type declarations related to shared data */
  51. /* typedef int datatype; */
  52. typedef char *datatype;
  53. /*r* struct datatype { *r*/
  54.   /*r* struct datatype *link; *r*/
  55.   /*r* int lng; *r*/
  56.   /*r* char data[11]; *r*/
  57.   /*r* }; *r*/
  58.  
  59. /* type declaration of pointer to shared data */
  60. /*i* typedef datatype *dataptr; *i*/
  61. typedef datatype *dataptr;
  62. /*r* typedef struct datatype *dataptr; *r*/
  63.  
  64. /* constant value to be assigned to shared memory */
  65. /*i* datatype shrconst = 11111; *i*/
  66. datatype shrconst = "AAAAA     ";
  67. /*r* struct datatype shrconst = { *r*/
  68.   /*r* NULL, *r*/
  69.   /*r* 11111, *r*/
  70.   /*r* "AAAAA     " *r*/
  71.   /*r* }; *r*/
  72.  
  73. /* pointer to shared data */
  74. dataptr bufptr;
  75.  
  76. /* mailbox semaphore controlling access to shared memory */
  77. ulong sema;
  78.  
  79. /* global name of mailbox semaphore */
  80. char name[] = "Shared Memory Semaphore";
  81.  
  82.  
  83. /**********************************************************************
  84. *  main  -  check for DESQview present and enable required extensions.
  85. ***********************************************************************/
  86.  
  87. main () {
  88.   /* initialize C interfaces and get API version number */
  89.   version = api_init();
  90.  
  91.   /* if DESQview is not running or version is too low, display a message */
  92.   if (version < required) {
  93.     printf ("This program requires DESQview version %d.02%d or later.\n",
  94.              required/256,required%256);
  95.     }
  96.  
  97.   else {
  98.  
  99.     /* tell DESQview what extensions to enable and start application */
  100.     api_level (required);
  101.     program_body();
  102.  
  103.     /* disable C interfaces and return from program */
  104.     api_exit();
  105.     }
  106.  
  107.   }
  108.  
  109.  
  110. /********************************************************************
  111. *  program_body  -  read, display and modify contents of shared data.
  112. ********************************************************************/
  113.  
  114. program_body () {
  115.  
  116.   /* get object handle */
  117.   win = win_me();
  118.  
  119.   /* create & name mailbox semaphore */
  120.   sema = mal_new();
  121.   mal_name (sema,name,sizeof (name));
  122.  
  123.   /* read other process' dvp file into buffer area */
  124.   fp = fopen (PIFNAME,"rb");
  125.   fread (dvpbuf,PIFLEN,1,fp);
  126.   fclose (fp);
  127.  
  128.   /* move current drive/path into DVP buffer */
  129.   getcwd (pathbuf,64);
  130.   dvpbuf[100] = pathbuf[0];
  131.   strcpy (&dvpbuf[101],&pathbuf[2]);
  132.  
  133.   /* start other process & get its task handle */
  134.   apphanb = app_start (dvpbuf,PIFLEN);
  135.  
  136.   /* allocate shared memory & get its buffer pointer */
  137.   /*i* bufptr = (dataptr) api_getmem (sizeof (datatype)); *i*/
  138.   bufptr = (dataptr) api_getmem (sizeof (datatype)); 
  139.   /*r* bufptr = (dataptr) api_getmem (sizeof (struct datatype)); *r*/
  140.  
  141.   /* copy initial data into shared memory */
  142.   /*i* *bufptr = shrconst; *i*/
  143.   strcpy (bufptr,shrconst);
  144.   /*r* bufptr->lng = shrconst.lng; *r*/
  145.   /*r* strcpy (bufptr->data,shrconst.data); *r*/
  146.  
  147.   /* mail to other process the pointer to shared data */
  148.   mal_write (mal_of (apphanb),&bufptr,4);
  149.  
  150.   /* disallow closing of window */
  151.   win_disallow (win,ALW_CLOSE);
  152.  
  153.   /* loop till handle of other process is no longer valid */
  154.   while (api_isobj (apphanb)) {
  155.  
  156.     /* lock semaphore */
  157.     mal_lock (sema);
  158.  
  159.     /* loop REPS times */
  160.     for (i = 1; i <= REPS; i++) {
  161.  
  162.       /* read & display current contents & address of shared data */
  163.       /*i* win_printf (win,"%i at %Fp\n",*bufptr,bufptr); *i*/
  164.       win_printf (win,"%s at %Fp\n",bufptr,bufptr);
  165.       /*r* win_printf (win,"%i %s at %Fp\n",bufptr->lng,bufptr->data,bufptr); *r*/
  166.  
  167.       /* modify contents of shared data */
  168.       /*i* *bufptr = shrconst; *i*/
  169.       strcpy (bufptr,shrconst);
  170.       /*r* bufptr->lng = shrconst.lng; *r*/
  171.       /*r* strcpy (bufptr->data,shrconst.data); *r*/
  172.       }
  173.  
  174.     /* unlock semaphore */
  175.     mal_unlock (sema);
  176.     }
  177.  
  178.   /* allow closing of window */
  179.   win_allow (win,ALW_CLOSE);
  180.  
  181.   /* free allocated shared memory */
  182.   api_putmem (bufptr);
  183.  
  184.   /* free allocated object */
  185.   mal_free (sema);
  186.   }
  187.